#include("../../layout/header.html")
#@layout()
#define content()
<head>
    <!--    <link rel="stylesheet" href="/static/module/city-picker/city-picker.css">-->
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格搜索栏 -->
            <form id="form" class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">名称:</label>
                        <div class="layui-input-inline">
                            <input name="name" class="layui-input"
                                   placeholder="请输入名称"/>
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">办学层次:</label>
                        <div id="levelRadio" name="level" class="layui-input-block"></div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">类型:</label>
                        <div id="typeRadio" class="layui-input-block"></div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">城市</label>
                        <div class="layui-input-inline" style="position: relative;max-width: 400px;">
                            <input id="citypicker" name="area" class="layui-input" readonly type="text">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="dataTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>
<!-- 表单弹窗 -->
<script type="text/html" id="dataEditDialog">
    <form id="dataEditForm" lay-filter="dataEditForm" class="layui-form model-form">
        <input name="id" type="hidden"/>

        <div class="layui-form-item">
            <label class="layui-form-label">省份:</label>
            <div class="layui-input-block">
                <select id="provinceId" name="provinceId">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">城市:</label>
            <div class="layui-input-block">
                <select id="cityId" name="cityId">
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">名称:</label>
            <div class="layui-input-block">
                <input name="name" class="layui-input" placeholder="请输入名称"/>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">办学层次:</label>
            <div id="level" name="level" class="layui-input-block"></div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">类型:</label>
            <div id="type" name="type" class="layui-input-block"></div>
        </div>


        <div class="layui-form-item text-right ai-form-footer">
            <button class="layui-btn" lay-filter="dataEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="dataTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'formX', 'laydate', 'citypicker'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var formX = layui.formX;
        var laydate = layui.laydate;
        var admin = layui.admin;
        var citypicker = layui.citypicker;
        var schoolLevel = [];
        var schoolType = [];

        admin.getDict('schoolLevel,schoolType', function (d) {
            if (d.code === 0) {
                schoolLevel = d.data.schoolLevel;
                schoolType = d.data.schoolType;
                formX.renderRadio({
                    elem: '#levelRadio',
                    data: schoolLevel,
                    name: 'level',
                    title: 'name',
                    value: 'value',
                })

                formX.renderRadio({
                    elem: '#typeRadio',
                    data: schoolType,
                    name: 'type',
                    title: 'name',
                    value: 'value',
                })
                /*最后渲染组件*/
                form.render();
            }
        })
        let cp = $("#citypicker").citypicker({level: 'city'});
        /* 渲染表格 */
        var insTb = table.render({
            elem: '#dataTable',
            url: '/fd/school/list',
            page: true,
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '<button lay-event="export" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>导出</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                {type: 'numbers'},
                {field: 'id', title: 'ID/学校代码'},
                {field: 'provinceName', title: '省份'},
                {field: 'cityName', title: '城市'},
                {field: 'name', title: '名称'},
                {field: 'levelStr', title: '办学层次'},
                {field: 'typeStr', title: '类型'},
                {title: '操作', toolbar: '#dataTbBar', align: 'center', width: 200, minWidth: 200}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(dataTbSearch)', function (data) {
            data.field.area = formX.getCityPickerLastValue('#citypicker');
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(dataTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('dataTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            admin.popupRight({
                type: 1,
                title: (mData ? '修改 ' : '添加 ') + ((mData && mData.name) ? mData.name : "全国高校信息"),
                content: $('#dataEditDialog').html(),
                success: function (layero, dIndex) {
                    formX.renderRadio({
                        elem: '#level',
                        data: schoolLevel,
                        name: 'level',
                        title: 'name',
                        value: 'value',
                    })
                    formX.renderRadio({
                        elem: '#type',
                        data: schoolType,
                        name: 'type',
                        title: 'name',
                        value: 'value',
                    })
                    // 回显表单数据
                    form.val('dataEditForm', mData);

                    // 表单提交事件
                    form.on('submit(dataEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);


                        admin.post('/fd/school/add', data.field, function (res) {
                            layer.close(loadIndex);
                            if (0 === res.code) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        });
                        return false;
                    });
                },
                end: function () {

                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                admin.req('/fd/school/del', {
                    id: obj.data ? obj.data.id : '',
                    ids: obj.ids ? obj.ids.join(',') : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (0 === res.code) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                });
            });
        }

    });
</script>
</body>
#end
